package com.paic.mhis.hcpms.common.baseFile.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.common.util.BusinessServiceUtil;
import com.paic.mhis.common.util.Constants;
import com.paic.mhis.hcpms.agreement.protocol.dto.FileInfoDTO;
import com.paic.mhis.hcpms.agreement.protocol.util.AgreementSqlMapIdConstants;
import com.paic.pafa.app.biz.service.BaseService;
import com.paic.pafa.app.biz.service.BusinessServiceException;

@Component("baseFileServiceUtil")
@Scope("prototype")
public class BaseFileServiceUtil extends BaseService {
	
	private static final Logger logger = Logger.getLogger(BaseFileServiceUtil.class);
	@Autowired
	private CommonDAO commonDao;

	public Map<String, Object> addImage(JSONObject transferJson, String currentUser, String fileName,long fileSize)
			throws BusinessServiceException {
		FileInfoDTO infoDTO = null;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			infoDTO = JSONObject.toJavaObject(transferJson, FileInfoDTO.class);
				String id = commonDao.getUUID();
				double file_Msize = fileSize/(1024.0*1024.0);
			    BigDecimal file_MsizeTemp = new BigDecimal(file_Msize);      
			    file_Msize = file_MsizeTemp.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				infoDTO.setImageIndex(id);
				infoDTO.setIsValid(BaseFileConstants.IS_VALID_TRUE);
				infoDTO.setImageStatus(BaseFileConstants.IS_VALID);
				infoDTO.setBusinessNo(infoDTO.getId());
				infoDTO.setImageName(fileName);
				infoDTO.setFileSize(String.valueOf(file_Msize)+"M");
				this.commonDao.insert(AgreementSqlMapIdConstants.IMAGEINFO_INSERT, infoDTO);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, BaseFileConstants.ADD_IMAGE_INFO_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	public Map<String, Object> getImage(String id) throws BusinessServiceException {
		Map<String, Object> infoDTO = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();
		List imgList = null;
		try {
			imgList = commonDao.query("healthcare.queryImgList", id);
			infoDTO.put("imgList", imgList);
			result.put(Constants.SUCCESS_RESULT_KEY, infoDTO);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	private void generateSuccessResult(Map<String, Object> result, String stateValue, String msgValue) {
		result.put(Constants.STATE_KEY, stateValue);
		result.put(Constants.MSG_KEY, msgValue);
	}
}
